const Service = require('../../../core/service/ApiService');


class DataService extends Service {

    //数据分析/服务对象统计
    async serviceObj(community, subdistrict) {
        const dao = this.service.admin.dataany.dao;
        const svcObj = await dao.serviceObj(community, subdistrict);
        const count = svcObj.count;
        const agedInfo = svcObj.rows;
        let num = 0;
        for (let i = 0; i < count; i++) {
            if (agedInfo[i].sex === 1)
                num++;
        }
        return {
            svc_obj_amount: count,
            male_amount: num,
            female_amount: count - num,
            age_distribution: await dao.getAgeGroup(community, subdistrict)
        }
    }

    //获取服务人员列表
    async getStaffList(town_id) {
        return {
            svc_staff_list: await this.service.admin.dataany.dao.getStaffList(town_id)
        }
    }

    //服务项目分析
    async analyzeSvcItem(begin, end, svc_staff_id, aged_id) {
        const dao = this.service.admin.dataany.dao;
        const begin_time = this.helper.YMDhms(begin);
        const end_time = this.helper.YMDhms(end);
        const [svcItemRateList, svcRecordAmountList, scoreRateList] = await this.helper.makePromises()
            .push(this.analyzeSvcItemRate(begin_time, end_time, svc_staff_id, aged_id))
            .push(dao.getServiceAmount(begin_time, end_time, svc_staff_id, aged_id))
            .push(dao.getScoreRate(begin_time, end_time, svc_staff_id, aged_id))
            .execute();
        return {
            svc_cnt_list: svcRecordAmountList,
            score_rate_list: scoreRateList,
            svc_item_rate_list: svcItemRateList
        }
    }

    async analyzeSvcItemRate(begin, end, svc_staff_id, aged_id) {
        const dao = this.service.admin.dataany.dao;
        const [svcItemMap, svcItemList] = await this.helper.makePromises()
            .push(this.getSvcItemMap(begin, end, svc_staff_id, aged_id))
            .push(dao.getSvcItemMetaList())
            .execute();
        for (let item of svcItemList) {
            item.cnt = svcItemMap[item.svc_item_id] || 0;
            delete item.svc_item_id
        }
        return svcItemList.sort((a, b) => {
            return a.cnt - b.cnt
        });
    }

    async getSvcItemMap(begin, end, svc_staff_id, aged_id) {
        const dao = this.service.admin.dataany.dao;
        const records = await dao.getSvcItemListOfSvcRecord(begin, end, svc_staff_id, aged_id);  //获取服务记录中的svc_item_list
        const svcItemMap = {};   // {svc_item_id: ${cnt}} map
        let cnt = 0;
        for (let record of records) {
            const svc_items = record.svc_item_list.split(',');
            for (let item of svc_items) {
                if (item !== '') {
                    ++cnt;
                    if (svcItemMap[item]) {
                        ++svcItemMap[item];
                    } else {
                        svcItemMap[item] = 1
                    }
                }
            }
        }
        return svcItemMap;
    }

}

module.exports = DataService;
